﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CloneFactoryTest
{
   public class Employee : Person
   {
      public Employee Manager { get; set; }
      public string EmailAddress { get; set; }

      public override string ToString()
      {
         return "Employee: " + NickName;
      }

      protected override int CalculateHashCode()
      {
         int hash = base.CalculateHashCode() << 1;
         hash ^= (EmailAddress ?? string.Empty).GetHashCode();
         hash = hash << 1;
         hash ^= (null == Manager) ? 27 : Manager.GetHashCode();
         return hash;
      }

      public override bool Equals(object obj)
      {
         return EqualEmployees(this, (Employee)obj, 2);
      }

      protected static bool EqualEmployees(Employee e1, Employee e2, int depth)
      {
         if (depth < 0)
         {
            return true;
         }

         if (null == e1 && null == e2) return true;
         if (null == e1 || null == e2) return false;

         --depth;

         bool equal = EqualPersons(e1, e2, depth) && e1.EmailAddress == e2.EmailAddress && Employee.EqualEmployees(e1.Manager, e2.Manager, depth);

         return equal;
      }
   }
}
